package com.magist.manage.web.controller;

import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.manage.web.dto.SysMenuDto;
import com.magist.manage.web.entity.SysMenu;
import com.magist.manage.web.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Api(value = "RestSysMenuController", description = "RestSysMenuController接口")
@RestController
@RequestMapping("/rest/sysMenu")
public class RestSysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "保存", notes = "保存接口")
    @RequestMapping(value = "save.do", method = RequestMethod.POST)
    public Object save(@ModelAttribute("sysMenuDto") SysMenuDto sysMenuDto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDto, sysMenu);
        sysMenu.setId(sysMenuService.getNewId());
        sysMenuService.insert(sysMenu);
        return ResultMessage.success();
    }

    @ApiOperation(value = "更新", notes = "更新接口")
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public Object update(@ModelAttribute("sysMenuDto") SysMenuDto sysMenuDto) {
        SysMenu oldSysMenu = sysMenuService.getById(sysMenuDto.getId());
        if (oldSysMenu == null)
            throw new BusinessException("无效的数据!");
        ResultMessage result = null;
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDto, sysMenu);
        sysMenu.setModifyTime(oldSysMenu.getModifyTime());
        if (sysMenuService.update(sysMenu) > 0)
            result = ResultMessage.success("操作成功");
        else
            result = ResultMessage.error("操作失败!");
        return result;
    }

    @ApiOperation(value = "获取树形表格数据json", notes = "获取树形表格数据json")
    @RequestMapping(value = "getTree.do", method = RequestMethod.GET)
    public Object getTree(@ModelAttribute("sysMenuDto") SysMenuDto sysMenuDto) {
        ResultMessage result = ResultMessage.success();
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDto, sysMenu);
        result.setContent(sysMenuService.getTreeJson(sysMenu));
        return result;
    }

    @ApiOperation(value = "删除", notes = "删除接口")
    @RequestMapping(value = "del.do", method = RequestMethod.POST)
    public Object del(@ModelAttribute("id") String id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        if (sysMenu == null)
            throw new BusinessException("无效的数据!");
        ResultMessage result = null;
        sysMenuService.delMenu(sysMenu);
        result = ResultMessage.success("操作成功");

        return result;
    }

    @ApiOperation(value = "详情", notes = "详情接口")
    @RequestMapping(value = "info.do", method = RequestMethod.GET)
    public Object info(@ModelAttribute("id") String id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        if (sysMenu == null)
            throw new BusinessException("无效的数据!");
        SysMenuDto sysMenuDto = new SysMenuDto();
        BeanUtils.copyProperties(sysMenu, sysMenuDto);
        ResultMessage result = ResultMessage.success();
        result.setContent(sysMenuDto);
        return result;
    }
}
